package com.gly091020;

import com.gly091020.util.ChineseChestWriteToDefault;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.stream.Stream;

import static com.gly091020.ChineseChest._5112151111121;

public class ChineseChestFun {
    public static class Poem{
        public String title;
        public String writer;
        public List<String> sentences;
        Poem(String t, String w, List<String> s){
            title = t;
            writer = w;
            sentences = s;
        }
    }
    private static List<Poem> poemList;
    public static Poem getRandomPoem(){
        if(Objects.isNull(poemList) || poemList.isEmpty()){
            return new Poem("Why?", "5112151111121", Arrays.stream((new String[]{
                    "你不应该看到这句话，", "如果看到了请报告到Github。"})).toList());
        }
        Collections.shuffle(poemList);
        return poemList.get(0);
    }

    private static final Map<Long, Float> itemMultiples = new HashMap<>();

    public static float getItemMultiple(long seed) {
        float m = itemMultiples.getOrDefault(seed, 1f);
        if(m < 0){m = 0;}
        return m;
    }

    public static void addItemMultiple(long seed, float m){
        itemMultiples.put(seed, m);
    }

    public static boolean hasSeed(long seed){
        return itemMultiples.containsKey(seed);
    }

    public static void removeMultiple(long seed){
        itemMultiples.remove(seed);
    }

    @SuppressWarnings("unchecked")
    public static List<Poem> convertJsonToPoems(Map<String, Map<String, Object>> jsonData) {
        List<Poem> poems = new ArrayList<>();

        for (Map.Entry<String, Map<String, Object>> entry : jsonData.entrySet()) {
            String title = entry.getKey();
            Map<String, Object> poemData = entry.getValue();

            String writer = (String) poemData.get("author");
            List<String> content = (List<String>) poemData.get("content");

            poems.add(new Poem(title, writer, content));
        }

        return poems;
    }

    public static ArrayList<String> getChineseFiles(){
        Path p = FabricLoader.getInstance().getConfigDir().resolve("ChineseChestFiles");
        initFiles();
        ArrayList<String> s = new ArrayList<>();
        try (Stream<Path> paths = Files.list(p)) {
            paths.forEach(path -> {
                if (!Files.isDirectory(path)) {
                    s.add(path.getFileName().toString());
                }
            });
        } catch (IOException e) {
            return new ArrayList<>();
        }
        return s;
    }

    public static void initFiles(){
        Path p = FabricLoader.getInstance().getConfigDir().resolve("ChineseChestFiles");
        try {
            Files.createDirectories(p);
        } catch (IOException ignored) {

        }
        ChineseChestWriteToDefault.writeToFile(p.resolve("default.json").toFile());
    }

    @SuppressWarnings("unchecked")
    public static void initPoems(Path f){
        initFiles();
        String json;
        try {
            json = Files.readString(f);
        } catch (IOException e) {
            return;
        }
        var l = new Gson().fromJson(json, new TypeToken<Map<String, Map<String, Object>>>(){}.getType());
        poemList = convertJsonToPoems((Map<String, Map<String, Object>>) l);
    }

    public static boolean isGLY(){
        return Objects.equals(MinecraftClient.getInstance().getSession().getUuidOrNull(), _5112151111121);
    }
}
